package com.pan.download2;

import com.pan.download2.entiry.ChapterInfo;
import org.openqa.selenium.edge.EdgeDriver;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;

/**
 * 只接收
 * www.kbwang.org 该网站的网小说
 */
public class Download {



    public void run(String bookName,String url){
        this.run(bookName,url,10);
    }

    public void run(String bookName,String url,Integer threadCount){

        List<ChapterInfo> chapterInfoList = getDirectory(url);
        Write write = new Write(bookName);
        //创建线程
        List<Thread> threadList = new ArrayList<>();
        for (Integer i = 0; i < threadCount; i++) {
            Page page = new Page(chapterInfoList,//目录信息
                    i,//开始章节
                    threadCount,//步长
                    write);//写入工具
            Thread thread = new Thread(page,"爬虫线程"+i);
            threadList.add(thread);
        }
        //将所有线程告诉write
        write.setThreadList(threadList);
        //开始运行
        threadList.forEach(Thread::start);
    }

    //目录的url
    public List<ChapterInfo> getDirectory(String url){
        Directory directory = new Directory(url);
        return directory.get();
    }

}
